package mh.demo.jdk.util.concurrent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

public class SemaphoreDemo {
    private static final int THREAD_COUNT = 30;
    private static final ExecutorService threadPool = Executors.newFixedThreadPool(THREAD_COUNT);
    private static final Semaphore s = new Semaphore(10);

    public static void main(String[] args) {
        for (int i = 0; i < THREAD_COUNT; i++) {
            final int j = i;
            threadPool.execute(() -> {
                try {
                    s.acquire();
                    TimeUnit.SECONDS.sleep(2);
                    System.out.println("save data" + j);
                    s.release();
                } catch (InterruptedException ignored) {
                }
            });
        }
        threadPool.shutdown();
    }
}
